package com.pig4cloud.pigx.shop.user.common.mq;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pig4cloud.pigx.common.core.constant.enums.MoneyChangeOperTypeEnum;
import com.pig4cloud.pigx.common.core.constant.enums.OperEnum;
import com.pig4cloud.pigx.common.core.constant.enums.PointChangeStatusEnum;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserInfo;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserMoneyChangeRecord;
import com.pig4cloud.pigx.shop.user.service.ShopUserInfoService;
import com.pig4cloud.pigx.shop.user.service.ShopUserMoneyChangeRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
@RocketMQMessageListener(topic = "shop-user-money-change", consumerGroup = "shop-user-money-group")
public class ShopUserMoneyChangeListener implements RocketMQListener<String> {

	private final ShopUserMoneyChangeRecordService shopUserMoneyChangeRecordService;

	private final ShopUserInfoService shopUserInfoService;

	/**
	 * 金额变更记录
	 * @param id
	 */
	@Override
	public void onMessage(String id) {
		log.info("金额变更信息: {}", id);
		try {
			Long moneyChangeId = Long.parseLong(id);
			ShopUserMoneyChangeRecord moneyChangeRecord = shopUserMoneyChangeRecordService.getById(moneyChangeId);
			if (moneyChangeRecord != null) {
				// 判断状态是不是处理中的状态
				if (!moneyChangeRecord.getStatus().equals(PointChangeStatusEnum.PROCESSING.getType())) {
					log.info("金额变更记录状态不是处理中状态: {}", id);
					return;
				}
				ShopUserInfo shopUserInfo = shopUserInfoService.getOne(Wrappers.<ShopUserInfo>lambdaQuery()
						.eq(ShopUserInfo::getShopUserId, moneyChangeRecord.getShopUserId()));
				if (shopUserInfo != null) {
					ShopUserInfo userInfo = new ShopUserInfo();
					userInfo.setId(shopUserInfo.getId());
					// 判断操作类别
					Integer balance = 0;
					if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.PROFIT_BALANCE.getType())) {
						// 分润钱包的场合
						log.info("分润钱包的场合: {}", id);
						balance = shopUserInfo.getProfitBalance();
					}
					else if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.CASH_BALANCE.getType())) {
						// 充值钱包的场合
						log.info("充值钱包的场合: {}", id);
						balance = shopUserInfo.getCashBalance();
					}
					else if (moneyChangeRecord.getOperType()
							.equals(MoneyChangeOperTypeEnum.USE_ORDER_TOTAL.getType())) {
						// 消费总额的场合
						log.info("消费总额的场合: {}", id);
						balance = shopUserInfo.getUseOrderTotal();
					}
					else if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.FREEZE_PROFIT.getType())) {
						// 冻结分润钱包余额
						log.info("冻结分润钱包余额的场合: {}", id);
						balance = shopUserInfo.getFreezeProfit();
					}
					else if (moneyChangeRecord.getOperType()
							.equals(MoneyChangeOperTypeEnum.EXTRACT_CASH_TOTAL.getType())) {
						// 提现总额的场合
						log.info("提现总额的场合: {}", id);
						balance = shopUserInfo.getExtractCashTotal();
					}
					log.info("金额变更记录ID:{}, 用户ID:{}, 变更前金额:{}", moneyChangeId, shopUserInfo.getShopUserId(), balance);
					if (moneyChangeRecord.getOper().equals(OperEnum.ADD.getType())) {
						// 添加操作
						balance += moneyChangeRecord.getMoney();
					}
					else {
						// 减少操作
						balance -= moneyChangeRecord.getMoney();
					}
					if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.PROFIT_BALANCE.getType())) {
						// 分润钱包的场合
						log.info("分润钱包的场合: {}", id);
						userInfo.setProfitBalance(balance);
					}
					else if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.CASH_BALANCE.getType())) {
						// 充值钱包的场合
						log.info("充值钱包的场合: {}", id);
						userInfo.setCashBalance(balance);
					}
					else if (moneyChangeRecord.getOperType()
							.equals(MoneyChangeOperTypeEnum.USE_ORDER_TOTAL.getType())) {
						// 消费总额的场合
						log.info("消费总额的场合: {}", id);
						userInfo.setUseOrderTotal(balance);
					}
					else if (moneyChangeRecord.getOperType().equals(MoneyChangeOperTypeEnum.FREEZE_PROFIT.getType())) {
						// 冻结分润钱包余额
						log.info("冻结分润钱包余额的场合: {}", id);
						userInfo.setFreezeProfit(balance);
					}
					else if (moneyChangeRecord.getOperType()
							.equals(MoneyChangeOperTypeEnum.EXTRACT_CASH_TOTAL.getType())) {
						// 提现总额的场合
						log.info("提现总额的场合: {}", id);
						userInfo.setExtractCashTotal(balance);
					}
					shopUserInfoService.updateById(userInfo);
					// 修改积分变更记录状态为成功
					moneyChangeRecord.setStatus(PointChangeStatusEnum.SUCCESS.getType());
					shopUserMoneyChangeRecordService.updateById(moneyChangeRecord);
					log.info("余额变更记录ID:{}, 用户ID:{}, 变更后金额:{}", moneyChangeId, shopUserInfo.getShopUserId(), balance);

				}
				else {
					log.info("用户不存在:" + moneyChangeId + "无法修改余额");
				}
			}
			else {
				log.info("不存在当前金额变更记录:" + moneyChangeId + "无法修改余额");
			}

		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
